# ==============================
# _*_ coding:utf-8 _*_
# _*_ by:zizle     _*_
# _*_ at:20230422  _*_
# ==============================
import datetime
import pathlib


class Logger:
    def __init__(self):
        self.fatal_folder = pathlib.Path('log/Fatal')
        self.error_folder = pathlib.Path('log/Error')
        self.infor_folder = pathlib.Path('log/Infor')

        if not self.fatal_folder.exists():
            self.fatal_folder.mkdir(parents=True)

        if not self.error_folder.exists():
            self.error_folder.mkdir(parents=True)

        if not self.infor_folder.exists():
            self.infor_folder.mkdir(parents=True)

    @staticmethod
    def get_now(is_time=False):
        fot = '%Y-%m-%d_%H%M%S' if is_time else '%Y-%m-%d'
        return datetime.datetime.now().strftime(fot)

    def fetal(self, fetal, prefix=''):
        fetal_file = self.fatal_folder.joinpath('{}{}.log'.format(prefix, self.get_now(is_time=True)))
        fetal_msg = '{}:{}'.format(self.get_now(), fetal)
        with open(fetal_file, 'w', encoding='utf8') as log:
            log.write(fetal_msg)

    def error(self, error, prefix=''):
        error_file = self.error_folder.joinpath('{}{}.log'.format(prefix, self.get_now(is_time=False)))
        error_msg = '{}:{}\n'.format(self.get_now(is_time=True), error)
        with open(error_file, 'a+', encoding='utf8') as log:
            log.write(error_msg)

    def infor(self, infor, prefix=''):
        infor_file = self.infor_folder.joinpath('{}{}.log'.format(prefix, self.get_now(is_time=False)))
        infor_msg = '{}:{}\n'.format(self.get_now(is_time=True), infor)
        with open(infor_file, 'a+', encoding='utf8') as log:
            log.write(infor_msg)